package com.lagou;

import com.lagou.bean.ConsumerComponent;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.scheduling.annotation.EnableScheduling;

import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;


public class AnnotationConsumerMain {
    static int threadNum = Runtime.getRuntime().availableProcessors();

    public static void main(String[] args) throws IOException, InterruptedException {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ConsumerConfiguration.class);
        context.start();
        ConsumerComponent service = context.getBean(ConsumerComponent.class);

        ExecutorService executorService = Executors.newFixedThreadPool(threadNum);


        TimeUnit.SECONDS.sleep(5);

        while (true) {
            TimeUnit.MILLISECONDS.sleep(20);

            executorService.execute(() -> {
                try {
                    service.methodA("Consumer", 1000);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });

            executorService.execute(() -> {
                try {
                    service.methodB("Consumer", 1000);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });

            executorService.execute(() -> {
                try {
                    service.methodC("Consumer", 1000);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });

//            doService( executorService,e->{
//                String hello = service.methodB("Consumer", e);
////                System.out.println("result :" + hello);
//            });
//            doService( executorService,e->{
//                String hello = service.methodC("Consumer", e);
////                System.out.println("result :" + hello);
//            });
        }
    }


    @Configuration
    @PropertySource("classpath:/dubbo-consumer.properties")
    //@EnableDubbo(scanBasePackages = "com.lagou.bean")
    @ComponentScan("com.lagou")
    @EnableDubbo
    @EnableScheduling
    static class ConsumerConfiguration {

    }


}
